//
//  PKReadDetailViewController.m
//  PianKe
//
//  Created by lanou on 15/12/28.
//  Copyright © 2015年 KFC. All rights reserved.
//

#import "PKReadDetailViewController.h"
#import "PKReadDetailListTableViewController.h"
#import "PKReadWebViewController.h"

@interface PKReadDetailViewController ()<UIScrollViewDelegate>

@property (strong, nonatomic) UIScrollView *mainScrollView;

@property (strong, nonatomic) PKReadDetailListTableViewController *ReadNewTableView;
@property (strong, nonatomic) PKReadDetailListTableViewController *ReadHotTableView;

// 动画的UIView
@property (strong, nonatomic) UIView *changeSortTypeView;
@property (strong, nonatomic) UIButton *myNewButton;
@property (strong, nonatomic) UIButton *myHotButton;


@end

@implementation PKReadDetailViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    // 不允许系统自动修改我们的frame
    self.automaticallyAdjustsScrollViewInsets = NO;
    
#pragma mark --- 自定义导航栏
    [self addBarView];
    
    
    _mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 70*kHeightBase, kScreenWidth, kScreenHeight - 70*kHeightBase)];
   // _mainScrollView.backgroundColor = [UIColor redColor];
    _mainScrollView.contentSize = CGSizeMake(kScreenWidth * 2, _mainScrollView.height);
    _mainScrollView.showsVerticalScrollIndicator = NO;
    _mainScrollView.showsHorizontalScrollIndicator = YES;
    _mainScrollView.pagingEnabled = YES;
    _mainScrollView.bounces = NO;
    _mainScrollView.delegate = self;
   
    [self.view addSubview:_mainScrollView];
    
    #pragma mark  ------ 第一页的new
    _ReadNewTableView = [[PKReadDetailListTableViewController alloc] init];
    // 先传值,再给tableView这个view赋值,不然传值在后面传,是请求不到数据的
    _ReadNewTableView.typeID = _myTypeId;
    _ReadNewTableView.sortType = @"addtime";
    _ReadNewTableView.tableView.frame = CGRectMake(0, 0, kScreenWidth, _mainScrollView.height);
    [_mainScrollView addSubview:_ReadNewTableView.tableView];
    

    #pragma mark  ------ 第二页的hot
    _ReadHotTableView = [[PKReadDetailListTableViewController alloc] init];
    _ReadHotTableView.typeID = _myTypeId;
    _ReadHotTableView.sortType = @"hot";
    _ReadHotTableView.tableView.frame = CGRectMake(kScreenWidth, 0, kScreenWidth, _mainScrollView.height);
    [_mainScrollView addSubview:_ReadHotTableView.tableView];
    
    
    // 添加一个回弹按钮
    UIButton *topButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    topButton.frame = CGRectMake(kScreenWidth - 40*kWidthBase,kScreenHeight - 40*kHeightBase, 30*kWidthBase, 30*kHeightBase);
    [topButton setBackgroundImage:[UIImage imageNamed:@"top"] forState:(UIControlStateNormal)];
    topButton.backgroundColor = [UIColor clearColor];
    [self.view addSubview:topButton];
    [_mainScrollView bringSubviewToFront:topButton];
    [topButton addTarget:self action:@selector(topAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    
    
}

- (void)addBarView
{
    // 创建自定义导航栏
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 64)];
    [self.view addSubview:headView];
    
    UIButton *leftBack = [UIButton buttonWithType:(UIButtonTypeCustom)];
    leftBack.frame = CGRectMake(10*kWidthBase, 30*kHeightBase, 30*kWidthBase, 30*kHeightBase);
    [leftBack setBackgroundImage:[UIImage imageNamed:@"fanhui"] forState:(UIControlStateNormal)];
    [headView addSubview:leftBack];
    [leftBack addTarget:self action:@selector(backAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    _myNewButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    _myNewButton.frame = CGRectMake(kScreenWidth - 30*kWidthBase - 150*kWidthBase, 34*kHeightBase, 30*kWidthBase, 30*kHeightBase);
    _myNewButton.backgroundColor = [UIColor blackColor];
    _myNewButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [_myNewButton setTitle:@"New" forState:(UIControlStateNormal)];
    [self.view addSubview:_myNewButton];
    
    _myHotButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    _myHotButton.frame = CGRectMake(kScreenWidth - 30*kWidthBase - 30*kWidthBase, 34*kHeightBase, 30*kWidthBase, 30*kHeightBase);
    _myHotButton.backgroundColor = [UIColor grayColor];
    _myHotButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [_myHotButton setTitle:@"Hot" forState:(UIControlStateNormal)];
    [self.view addSubview:_myHotButton];
    
    _changeSortTypeView = [[UIView alloc] initWithFrame:CGRectMake(_myNewButton.left + 5*kWidthBase, 65*kHeightBase, 20*kWidthBase, 2*kWidthBase)];
    _changeSortTypeView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:_changeSortTypeView];
    
    
    [_myNewButton addTarget:self action:@selector(hotToNew:) forControlEvents:(UIControlEventTouchUpInside)];
     [_myHotButton addTarget:self action:@selector(newToHot:) forControlEvents:(UIControlEventTouchUpInside)];
    
    

    
    
 
    // 注册通知
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(notification:)name:@"BBB"object:nil];
    
    
}



- (void)topAction:(UIButton *)button
{
    NSLog(@"top");
    
     NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    
    if (_mainScrollView.contentOffset.x < kScreenWidth) {
        
        // 说明还在第一页,
       
        [_ReadNewTableView.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
    } else
    {
        [_ReadHotTableView.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:(UITableViewScrollPositionTop) animated:YES];
    }
 
}



// 实现通知的方法
- (void)notification:(NSNotification *)notifition
{
    NSLog(@"通知的名字:%@,谁发的通知:%@,想传递的信息:%@",notifition.name,notifition.object,notifition.userInfo);
    
    PKReadWebViewController *webView = [[PKReadWebViewController alloc] init];
    
    webView.url = notifition.userInfo[@"url"];
    
    [self.navigationController pushViewController:webView animated:YES];
    
    // 注销通知
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"BBB"object:nil];
    
    
}

- (void)viewWillAppear:(BOOL)animated
{
    
    // 再注册通知
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(notification:)name:@"BBB"object:nil];
    
}


// 点击hot按钮
- (void)newToHot:(UIButton *)button
{
    
    
    [_mainScrollView setContentOffset:CGPointMake(kScreenWidth, 0) animated:YES];
    _myHotButton.backgroundColor = [UIColor blackColor];
    _myNewButton.backgroundColor = [UIColor grayColor];
    
    
    [UIView animateWithDuration:0.5 animations:^{
       
    _changeSortTypeView.frame = CGRectMake(_myNewButton.left + 5*kWidthBase +  120*kWidthBase, _changeSortTypeView.top, _changeSortTypeView.width, _changeSortTypeView.height);
        
    }];
    
}

// 点击new按钮
- (void)hotToNew:(UIButton *)button
{
    
    
    [_mainScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    _myHotButton.backgroundColor = [UIColor grayColor];
    _myNewButton.backgroundColor = [UIColor blackColor];
    
    
    [UIView animateWithDuration:0.5 animations:^{
        
        _changeSortTypeView.frame = CGRectMake(_myNewButton.left + 5*kWidthBase , _changeSortTypeView.top, _changeSortTypeView.width, _changeSortTypeView.height);
        
    }];
    
}

// 滑动结束触发的方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{

    
    CGPoint point = scrollView.contentOffset;
    
    NSLog(@"%@",NSStringFromCGPoint(point));
    

    
    if (point.x == kScreenWidth) {
        
        // 去hot
        
        [UIView animateWithDuration:0.5 animations:^{
            
            _changeSortTypeView.frame = CGRectMake(_myNewButton.left + 5*kWidthBase +  120*kWidthBase, _changeSortTypeView.top, _changeSortTypeView.width, _changeSortTypeView.height);
            
        }];
        
        _myHotButton.backgroundColor = [UIColor blackColor];
        _myNewButton.backgroundColor = [UIColor grayColor];
        

        
    }
    
     else if (point.x == 0) {
        
         NSLog(@"回来来");
         
        [UIView animateWithDuration:0.5 animations:^{
            
            _changeSortTypeView.frame = CGRectMake(_myNewButton.left + 5*kWidthBase, _changeSortTypeView.top, _changeSortTypeView.width, _changeSortTypeView.height);
            
        }];

         _myHotButton.backgroundColor = [UIColor grayColor];
         _myNewButton.backgroundColor = [UIColor blackColor];

    }
    
}






// 实现返回
- (void)backAction:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];

}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
